﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Scheduler.UI;
using Caliburn.Micro;
using DevExpress.XtraScheduler;

namespace Agnes.TaskManager
{
    /// <summary>
    /// TaskCalendarView.xaml 的交互逻辑
    /// </summary>
    public partial class TaskCalendarView : UserControl
    {
        public TaskCalendarView()
        {
            InitializeComponent();
        }

        public void ReLoaded()
        { 
            //AStorage.DataSource = null;
            //AppointmentStorage.DataSource = obj;
        }

        private void EditTask_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            try
            {
                Task selTask = null;
                Appointment app = SchedulerControl.SelectedAppointments[0];
                Guid id = Guid.Parse(app.Location);
                foreach (Task i in (this.DataContext as TaskCalendarViewModel).Model.LeafTasks)
                {
                    if (i.Id == id)
                    {
                        selTask = i;
                        break;
                    }
                }

                TaskEditorViewModel taskEditorViewModel = new TaskEditorViewModel(selTask, true);
                IWindowManager windowManager = IoC.Get<IWindowManager>();
                selTask.BeginEdit();

                if (windowManager.ShowDialog(taskEditorViewModel) == true)
                {
                    selTask.ApplyEdit();
                }
                else
                {
                    selTask.CancelEdit();
                }
                selTask.Save();
                //TaskManagerService.TaskManager.LeafTasks.Remove(selTask);
                //TaskManagerService.TaskManager.LeafTasks.Add(selTask);
            }
            catch (Exception ex)
            {

            }
        }

        private void SchedulerControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            e.Handled = true;

        }

        private void SchedulerControl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void SchedulerControl_PopupMenuShowing(object sender, DevExpress.Xpf.Scheduler.SchedulerMenuEventArgs e)
        {
            e.Handled = true;
        }

        private void SchedulerControl_AllowAppointmentEdit(object sender, AppointmentOperationEventArgs e)
        {
            e.Allow = true;
        }

        private void SchedulerControl_AllowAppointmentDelete(object sender, AppointmentOperationEventArgs e)
        {
            e.Allow = false;
        }

        private void SchedulerControl_EditAppointmentFormShowing(object sender, DevExpress.Xpf.Scheduler.EditAppointmentFormEventArgs e)
        {
            EditTask_ItemClick(null, null);
            e.Cancel = true;
        }
    }
}
